<?php
error_reporting(0);
function customError($errno, $errstr, $errfile, $errline) {
     echo "<b>Custom error:</b> [$errno] $errstr<br>";
     echo " Error on line $errline in $errfile<br>";
	 die();
 }
 
//set_error_handler("customError");
require dirname(__FILE__).'/init.php';
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("content-type:text/html; charset=utf-8");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:GET,POST");
if ($_SERVER["HTTP_HOST"]!="sudoku.bbaass.tk"&&$_SERVER["HTTP_HOST"]!="127.0.0.1") {
	header("HTTP/1.1 301 Moved Permanently");
	header("Location: http://sudoku.bbaass.tk/");
}
/* 函数放置部分 Start */
function random_str($length) {
	$str='';
	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';   
	for ($i = 0; $i < $length; $i++ ) {
		$str .=substr($chars,mt_rand(0,strlen($chars)-1),1);  
		//$str = $chars[ mt_rand(0, strlen($chars) - 1) ];  
	}  
	return $str;  
}
/* 函数放置部分 End */
/* 开始自运行部分 Start */
/* 开始自运行部分 End */
if (!empty($_POST["Send"])) {
	switch($_POST["Send"]) {
		case "reg":
			if (isset($_POST["Username"])&&isset($_POST["Pwd"])) {
				if(!$m->query("INSERT INTO `sudoku_users` (Username, PWd,Tokens) VALUES ('".$m->escape_string($_POST["Username"])."','".Sha1($_POST["Pwd"])."','".Sha1(random_str(256))."')",true)) {
					die(json_encode(array("Code"=>400)));
				} else {
					die(json_encode(array("Code"=>200)));
				}
			}
			die(json_encode(array("Code"=>400)));
		break;
		case "Sudoku":
			$r=new Sudoku();
			$zhpan=$r->GetSudoku();
			$timu=$r->Randomc($zhpan,mt_rand(10,45));
			die(json_encode(array("Code"=>200,"Sudoku"=>$r->toString($timu),"zp"=>$r->toString($zhpan))));
		break;
		case "check":
			if (isset($_POST["Sudoku"])) {
				$r=new Sudoku();
				if ($r->check($_POST["Sudoku"])) {
					die(json_encode(array("Code"=>200)));
				} else {die(json_encode(array("Code"=>400)));}
			} else {die(json_encode(array("Code"=>400)));}
		break;
		default:
			die(json_encode(array("Code"=>200)));
		break;
	}
}
if (empty($_POST["Send"])) {
?>
<html>
	<head>
		<!-- Author:Bbaa -->
		<meta charset="UTF-8">
		<title>
			数独计算比赛
		</title>
		<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js">
		</script>
		<link href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
		<link href="//bbaass.tk/Sudoku/Sudoku.css.php" rel="stylesheet">
		<script src="https://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js">
		</script>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
		<style type="text/css">
			body {
				font-family: "微软雅黑","Microsoft YaHei";
				background:#eee;
			}
			.playing {
				color: #1ce223;
			}
			.played {
				color: red;
			}
			.noselect {
				-moz-user-select: none;
				-webkit-user-select: none;
				user-select: none;
			}
			.select{
				-moz-user-select: text;
				-webkit-user-select:text;
				user-select: text;
			} 
			.badge_lv4 .badge_index {
				background: url(http://tb2.bdstatic.com/tb/img/s8_ab1dc3e.gif) no-repeat scroll 3px 1px transparent;
				padding-top: 2px
			}
			.badged {
				position: relative;
				float: left;
				width: 30px;
				cursor: pointer;
				height: 28px;
				line-height: 28px;
				background: #fff
			}
			.badge_index {
				position: absolute;
				width: 30px;
				padding-left: 11px;
				color: #B65E00;
				text-align: left;
				font-weight: 700
			}
		</style>
		<script>
			function GetSudoInput() {
				var Sudoku=new Array();
				$(".sd").children().children().each(function(){
					Sudoku.push($(this).text());
				});
				return Sudoku.join("");
			}
			function getCookie(c_name) {
				if (document.cookie.length>0) {
					c_start=document.cookie.indexOf(c_name + "=");
					if (c_start!=-1) { 
						c_start=c_start + c_name.length+1 ;
						c_end=document.cookie.indexOf(";",c_start);
						if (c_end==-1) c_end=document.cookie.length;
						return unescape(document.cookie.substring(c_start,c_end));
					}
				}
			return false;
			}
			function setCookie(c_name,value,expiredays) {
				var exdate=new Date();
				exdate.setDate(exdate.getDate()+expiredays);
				document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
			}
			$(document).ready(function() {
				<?php if(isset($_COOKIE["Username"])) {echo '$("div#button").show();';} ?>
				//if (getCookie("Username")!=false) {}
	/* 			$("form").submit(function() {
					$.post("index.php",{"Username":$("#User").val(),"Pwd":$("#Pwd").val(),"Send":"reg"},function(data){
						if (data.Code==200) {
							setCookie("Username",$("#User").val(),365);
							$("div#button").show();
							$("form").remove();
							
						} else {
							$("#sc").removeClass("btn-primary");
							$("#sc").addClass("btn-danger");
							alert("已经有人注册");
						}
					},"json");
				}); */
				$("#se").click(function() {
					$("#se").text("Loading...");
					$.post("index.php",{"Send":"Sudoku"},function(data){
						if (data.Code==200) {
							$("#se").text("切换题目");
							$("#timu").text(data.Sudoku);
							$("#zpan").text(data.zp);
							var SudokuArr=data.Sudoku;
							SudokuArr=SudokuArr.split("");
							$(".sd").children().children().each(function(Count){
								if (SudokuArr[Count]==0) {
									$(this).addClass("blankCell");
									$(this).text(null);
									$(this).attr("contenteditable","true");
									$(this).keyup(function() {
										if($(this).text().search(/\n/)!=-1||$(this).text()==0) {$(this).text("")}
										if(isNaN($(this).text())) {
											$(this).text("")
										}
									});
									$(this).keypress(function() {
										if($(this).text().search(/\n/)!=-1||$(this).text()==0) {$(this).text("")}
										if(isNaN($(this).text())) {
											$(this).text("")
										} else {
											if($(this).text().length>0) {
												$(this).text($(this).text().substr($(this).text().length,1));
											}
										}
									});
								} else {
									$(this).removeClass("blankCell");
									$(this).removeAttr("contenteditable");
									$(this).text(SudokuArr[Count]);
								}
							});
						} else {
							$("#se").text("Error");
						}
					},"json");
				});
				$("#sch").click(function() {
					$("#sch").fadeOut(200);
					$.post("index.php",{"Send":"check","Sudoku":GetSudoInput()},function(data){
						$("#sch").fadeIn(500);
						if (data.Code==200) {
							$("#sch").removeClass("btn-primary btn-danger");
							$("#sch").addClass("btn-success");
						} else {
							$("#sch").removeClass("btn-primary btn-success");
							$("#sch").addClass("btn-danger");
						}
					},"json");
				});
			});
		</script>
	</head>
	<body>
		<div class="container bs-docs-container">
			<div class="row">
				<div class="noselect panel panel-primary" style="margin:1% 1% 1% 1%;">
					<div class="panel-heading">
						<h3 class="panel-title">
							数独题目
						</h3>
					</div>
					<div id="bd" style="margin:0% 5% 5% 5%;">
						<br/>
						<div class="login-top">
							<div class="alert alert-success"><div id="Sudoku">题目:<span id="timu" class="select"></span></div></div>
							<div class="alert alert-success"><div id="Sudoku1">终盘:<span id="zpan" class="select"></span></div></div>
						</div>
					</div>
				</div>
				<div class="noselect panel panel-primary" style="margin:1% 1% 1% 1%;height:500px">
					<!-- <div class="panel-heading">
						<h3 class="panel-title">
							数独盘
						</h3>
					</div> -->
					<div id="bd" style="margin:0% 5% 5% 5%;">
						<ul class="sd">
							<li class="sdli">
								<span class="sdspan sdzs"></span>
								<span class="sdspan"></span>
								<span class="sdspan br"></span>
								<span class="sdspan bl"></span>
								<span class="sdspan"></span>
								<span class="sdspan br"></span>
								<span class="sdspan bl"></span>
								<span class="sdspan"></span>
								<span class="sdspan sdys"></span>
							</li>
							<li class="sdli">
								<span class="sdspan"></span>
								<span class="sdspan"></span>
								<span class="sdspan br"></span>
								<span class="sdspan bl"></span>
								<span class="sdspan"></span>
								<span class="sdspan br"></span>
								<span class="sdspan bl"></span>
								<span class="sdspan"></span>
								<span class="sdspan"></span>
							</li>
							<li class="sdli">
								<span class="sdspan bb"></span>
								<span class="sdspan bb"></span>
								<span class="sdspan bb br"></span>
								<span class="sdspan bb bl"></span>
								<span class="sdspan bb"></span>
								<span class="sdspan bb br"></span>
								<span class="sdspan bb bl"></span>
								<span class="sdspan bb"></span>
								<span class="sdspan bb"></span>
							</li>
							<li class="sdli">
								<span class="sdspan bt"></span>
								<span class="sdspan bt"></span>
								<span class="sdspan bt br"></span>
								<span class="sdspan bt bl"></span>
								<span class="sdspan bt"></span>
								<span class="sdspan bt br"></span>
								<span class="sdspan bt bl"></span>
								<span class="sdspan bt"></span>
								<span class="sdspan bt"></span>
							</li>
							<li class="sdli">
								<span class="sdspan"></span>
								<span class="sdspan"></span>
								<span class="sdspan br"></span>
								<span class="sdspan bl"></span>
								<span class="sdspan"></span>
								<span class="sdspan br"></span>
								<span class="sdspan bl"></span>
								<span class="sdspan"></span>
								<span class="sdspan"></span>
							</li>
							<li class="sdli">
								<span class="sdspan bb"></span>
								<span class="sdspan bb"></span>
								<span class="sdspan bb br"></span>
								<span class="sdspan bb bl"></span>
								<span class="sdspan bb"></span>
								<span class="sdspan bb br"></span>
								<span class="sdspan bb bl"></span>
								<span class="sdspan bb"></span>
								<span class="sdspan bb"></span>
							</li>
							<li class="sdli">
								<span class="sdspan bt"></span>
								<span class="sdspan bt"></span>
								<span class="sdspan bt br"></span>
								<span class="sdspan bt bl"></span>
								<span class="sdspan bt"></span>
								<span class="sdspan bt br"></span>
								<span class="sdspan bt bl"></span>
								<span class="sdspan bt"></span>
								<span class="sdspan bt"></span>
							</li>
							<li class="sdli">
								<span class="sdspan"></span>
								<span class="sdspan"></span>
								<span class="sdspan br"></span>
								<span class="sdspan bl"></span>
								<span class="sdspan"></span>
								<span class="sdspan br"></span>
								<span class="sdspan bl"></span>
								<span class="sdspan"></span>
								<span class="sdspan"></span>
							</li>
							<li class="sdli">
								<span class="sdspan sdzx"></span>
								<span class="sdspan"></span>
								<span class="sdspan br"></span>
								<span class="sdspan bl"></span>
								<span class="sdspan"></span>
								<span class="sdspan br"></span>
								<span class="sdspan bl"></span>
								<span class="sdspan"></span>
								<span class="sdspan sdyx"></span>
							</li>
						</ul>
					</div>
				</div>
				<div class="noselect panel panel-primary" style="margin:1% 1% 1% 1%;">
					<div id="bd" style="margin:0% 5% 5% 5%;">
						<div id="button" style="display:block;">
							<br />
							<button id="se" type="submit" class="btn btn-primary" style="width:48%;float:left;">切换题目</button>
							<button id="sch" type="submit" class="btn btn-primary" style="width:48%;float:right;">检测数独</button>
						</div>
					</div>
				</div>
				<ul class="list-group" style="display :none">
				</ul>
			</div>
		</div>
	</body>
</html>
<?php } ?>